package com.example.tally.frag_char;

import android.graphics.Color;
import android.view.View;
import androidx.fragment.app.Fragment;
import com.example.tally.db.BarChartItemBean;
import com.example.tally.db.DBManager;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class OutcomeCharFragment extends BaseCharFragment {
    int kind = 0;
    @Override
    public void onResume() {
        super.onResume();
        loaData(year , month , kind);
    }

    @Override
    protected void setAxisData(int year, int month) {
        List<IBarDataSet> sets = new ArrayList<>();
        // 获取这个月每一天的收入总数
        List<BarChartItemBean> list = DBManager.getSumMoneyOneDayInMonth(year, month, kind);
        if(list.size()==0){
            barChart.setVisibility(View.GONE);
            textView.setVisibility(View.VISIBLE);
        }else{
            barChart.setVisibility(View.VISIBLE);
            textView.setVisibility(View.GONE);
            // 设置多少根柱子
            List<BarEntry> barEntries = new ArrayList<>();
            for(int i = 0 ; i < 31;i++){
                // 初始化每一根柱子，添加到柱状图当中
                BarEntry entry = new BarEntry(i, 0.0f);
                barEntries.add(entry);
            }

            for (int i = 0; i < list.size(); i++) {
                BarChartItemBean bean = list.get(i);
                int day = bean.getDay();
                int index = day - 1;
                BarEntry entry = barEntries.get(index);
                entry.setY(bean.getSumMoney());
            }
            BarDataSet barDataSet1 = new BarDataSet(barEntries, "");
            barDataSet1.setValueTextColor(Color.BLACK); // 值的颜色
            barDataSet1.setValueTextSize(8f); // 值的大小
            barDataSet1.setColor(Color.parseColor("#006400")); // 柱子的颜色

            // 设置柱子上数据显示的格式
            barDataSet1.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                    // 此处的value默认保存一位小数
                    if(value == 0){
                        return "";
                    }
                    return value + "";
                }
            });
            sets.add(barDataSet1);

            BarData barData = new BarData(sets);
            barData.setBarWidth(0.2f); // 设置柱子的宽度
            barChart.setData(barData);
        }
    }

    @Override
    protected void setYAxis(int year, int month) {
        // 获取本月收入最高的一天为多少，将他设定为Y轴的最大值
        float maxMoney = DBManager.getMaxMoneyOneDayInMonth(year, month, kind);
        // 将最大金额向上取整
        float max = (float) Math.ceil(maxMoney);
        // 设置Y轴的数据
        // 设置y轴，y轴有两条，分别为左和右
        YAxis yAxis_right = barChart.getAxisRight();
        yAxis_right.setAxisMaximum(max);  // 设置y轴的最大值
        yAxis_right.setAxisMinimum(0f);  // 设置y轴的最小值
        yAxis_right.setEnabled(false);  // 不显示右边的y轴

        YAxis yAxis_left = barChart.getAxisLeft();
        yAxis_left.setAxisMaximum(max);
        yAxis_left.setAxisMinimum(0f);
        yAxis_left.setEnabled(false); // 设置不显示左边Y轴

        // 设置不显示图例
        Legend legend = barChart.getLegend();
        legend.setEnabled(false);
    }


    @Override
    public void setDate(int year, int month) {
        super.setDate(year, month);
        loaData(year , month , kind);
    }
}